package com.taotao.rest.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.mapper.TbItemCatMapper;
import com.taotao.pojo.TbItemCat;
import com.taotao.pojo.TbItemCatExample;
import com.taotao.pojo.TbItemCatExample.Criteria;
import com.taotao.rest.pojo.CatNode;
import com.taotao.rest.pojo.CatResult;
import com.taotao.rest.service.ItemCatService;
@Service
public class ItemCatServiceImpl implements ItemCatService {
	@Autowired
	private TbItemCatMapper itemCatMapper;
	@Override
	public CatResult getItemCatList() {
		CatResult catResult=new CatResult();
		catResult.setData(getCatList(0));
		return catResult;
	}
	private List<?> getCatList(long parentId) {
		TbItemCatExample itemCatExample=new TbItemCatExample();
		Criteria criteria = itemCatExample.createCriteria();
		criteria.andParentIdEqualTo(parentId);
		List<TbItemCat> list = itemCatMapper.selectByExample(itemCatExample);
		List catResult=new ArrayList<>();
		int count=0;
		for(TbItemCat itemCat:list){
			if(itemCat.getIsParent()){
				CatNode catNode=new CatNode();
				if(parentId==0){
					catNode.setName("<a href='/products/"+itemCat.getId()+".html'>"+itemCat.getName()+"</a>");
				}else{
					catNode.setName(itemCat.getName());
				}
					catNode.setUrl("/products/"+itemCat.getId()+".html");
					catNode.setItem(getCatList(itemCat.getId()));
					catResult.add(catNode);
					count++;
					if(count>=14&&itemCat.getParentId()==0){
						break;
					}
				}
			else{
				catResult.add("/products/"+itemCat.getId()+".html|" + itemCat.getName());
			}
		}
		return catResult;
	}

}
